import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.lang.Math;

public class Board
{
	char[][] board;
	int size;
	int offset;
	
	int[] lastMove;
	
	Image boardImage;
	
	String backGround;
	String greenP;
	String blueP;
	
	public Board(Board b)
	{
		if(b.size == 6)
		{
			board = new char[6][6];
			backGround = "Game\\smallBoard.jpg";
			greenP = "Game\\greenPS.gif";
			blueP = "Game\\bluePS.gif";
			offset = 67;
			size = 6;
		}
		
		else if(b.size == 8)
		{		
			board = new char[8][8];
			backGround = "Game\\boardMedium.jpg";
			greenP = "Game\\greenPM.gif";
			blueP = "Game\\bluePM.gif";
			offset = 50;
			size = 8;
		}
		for(int x = 0; x < size; x++)
        {
        	for(int y = 0; y < size; y++)
        	{
        		board[x][y] = b.board[x][y];
        	}
        }
        lastMove = b.lastMove;
        drawBoard();
	}
	
	public Board(int s)
	{		
		if(s == 6)
		{
			board = new char[6][6];
			backGround = "Game\\smallBoard.jpg";
			greenP = "Game\\greenPS.gif";
			blueP = "Game\\bluePS.gif";
			offset = 67;
			size = 6;
		}
		
		else if(s == 8)
		{		
			board = new char[8][8];
			backGround = "Game\\boardMedium.jpg";
			greenP = "Game\\greenPM.gif";
			blueP = "Game\\bluePM.gif";
			offset = 50;
			size = 8;
		}
		lastMove = new int[2];
		initalizeBoard();
	}
	
	public void initalizeBoard()
	{
		for(int x = 0; x < size; x++)
        {
        	for(int y = 0; y < size; y++)
        	{
        		board[x][y] = 'o';
        	}
        }
        
        if(size == 6)
        {
        	
        }
        else if(size == 8)
        {
        	board[3][3] = 'g';
        	board[4][3] = 'b';
        	board[3][4] = 'b';
        	board[4][4] = 'g';
        }
        
        drawBoard();
    }
	
	public void drawBoard()
	{
		BufferedImage pic = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
		
		Graphics gb = pic.createGraphics();
		
		Image p = new ImageIcon(backGround).getImage();        			
        gb.drawImage(p, 0, 0, null);
        
        for(int x = 0; x < size; x++)
        {
        	for(int y = 0; y < size; y++)
        	{
        		if(board[x][y] == 'g')
        		{
        			p = new ImageIcon(greenP).getImage();
        			gb.drawImage(p, (x*offset)+4, (y*offset)+6, null);
        		}
        		else if(board[x][y] == 'b')
        		{
        			p = new ImageIcon(blueP).getImage();
        			gb.drawImage(p, (x*offset)+4, (y*offset)+6, null);
        		}
        	}
        } 
        //System.out.println("drawing board");      
        boardImage = pic;		
	}
	
	public boolean isIn(int mX, int mY)
	{
		if(mX >= 200 && mX <= 600)
		{
			if(mY >= 50 && mY <= 450)
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean move(int turn, int mX, int mY)
	{
		//System.out.println("trying to move");
		int x, y;
		
		char piece;
		
		if(turn == 1)
			piece = 'g';
		else
			piece = 'b';
		
		x = (int)Math.ceil((mX - 200) / offset);
		
		y = (int)Math.ceil((mY - 50) / offset);
		//System.out.println("x: "+x+", y: "+y);
		
		if(validMove(piece, x, y))
		{
			place(piece, x, y);
			drawBoard();
			return true;
		}		
		return false;
	}
	
	public boolean moveAI(int turn, int x, int y)
	{
		char piece;
		
		if(turn == 1)
			piece = 'g';
		else
			piece = 'b';
				
		if(validMove(piece, x, y))
		{
			place(piece, x, y);
			drawBoard();
			return true;
		}		
		return false;
	}
	
	public void showHelp(int turn)
	{
		
	}
	
	public boolean validMove(char p, int x, int y)
	{		
		if ((x == 8) || (y == 8))
			return false;
		if(board[x][y] == 'o' || board[x][y] == 'x' )
		{
			
			if(checkUp(p, x, y))
				return true;
			
			if(checkRight(p, x, y))
				return true;
			
			if(checkDown(p, x, y))
				return true;
						
			if(checkLeft(p, x, y))
				return true;
			
			if(checkUpRight(p, x, y))
				return true;
			
			if(checkUpLeft(p, x, y))
				return true;
			
			if(checkDownRight(p, x, y))
				return true;
			
			if(checkDownLeft(p, x, y))
				return true;
						
		}				
		return false;
	}
	
	public boolean checkValidMoves(int turn)
	{	
		char p;
		
		if(turn == 1)
			p = 'g';
		else
			p = 'b';
	
		for(int x = 0; x < size; x++)
		{
			for(int y = 0; y < size; y++)
			{
				if(validMove( p, x, y))
				{
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean isFull()
	{
		for(int x = 0; x < size; x++)
		{
			for(int y = 0; y < size; y++)
			{
				if(board[x][y] != 'g' && board[x][y] != 'b')
				{
					return false;
				}
			}
		}
		return true;
	}
	
	public int count(int turn)
	{
		int total = 0;
		char p;
		
		if(turn == 1)
			p = 'g';
		else
			p = 'b';
		
		for(int x = 0; x < size; x++)
		{
			for(int y = 0; y < size; y++)
			{
				if(board[x][y] == p)
				{
					total++;
				}
			}
		}
		return total;
	}
		
	public void place(char p, int x, int y)
	{
		board[x][y] = p;
		
		lastMove[0] = x;
		lastMove[1] = y;
		
		if(checkUp(p, x, y))
			flipUp(p, x, y);
			
		if(checkRight(p, x, y))
			flipRight(p, x, y);
		
		if(checkDown(p, x, y))
			flipDown(p, x, y);
					
		if(checkLeft(p, x, y))
			flipLeft(p, x, y);
			
		if(checkUpRight(p, x, y))
			flipUpRight(p, x, y);
			
		if(checkUpLeft(p, x, y))
			flipUpLeft(p, x, y);
			
		if(checkDownRight(p, x, y))
			flipDownRight(p, x, y);
			
		if(checkDownLeft(p, x, y))
			flipDownLeft(p, x, y);
			
	}
	
	public boolean checkUp(char p, int x, int y)
	{
		y -= 1;
		boolean gotOne = false;
		while(y >= 0)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			y--;
		}
		return false;
	}
	public boolean checkRight(char p, int x, int y)
	{
		x += 1;
		boolean gotOne = false;
		while(x < size)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			x++;
		}
		return false;
	}
	public boolean checkLeft(char p, int x, int y)
	{
		x -= 1;
		boolean gotOne = false;
		while(x >= 0)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			x--;
		}
		return false;
	}
	public boolean checkDown(char p, int x, int y)
	{
		y += 1;
		boolean gotOne = false;
		while(y < size)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			y++;
		}
		return false;
	}
	public boolean checkUpRight(char p, int x, int y)
	{
		y -= 1;
		x += 1;
		boolean gotOne = false;
		while(y >= 0 && x < size)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			y--;
			x++;
		}
		return false;
	}
	public boolean checkUpLeft(char p, int x, int y)
	{
		y -= 1;
		x -= 1;
		boolean gotOne = false;
		while(y >= 0 && x >= 0)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			y--;
			x--;
		}
		return false;
	}
	public boolean checkDownRight(char p, int x, int y)
	{
		y += 1;
		x += 1;
		boolean gotOne = false;
		while(y < size && x < size)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			y++;
			x++;
		}
		return false;
	}
	public boolean checkDownLeft(char p, int x, int y)
	{
		y += 1;
		x -= 1;
		boolean gotOne = false;
		while(y < size && x >= 0)
		{
			if(!gotOne && board[x][y] != p && board[x][y] != 'o')
			{
				gotOne = true;
			}
			else if(gotOne && board[x][y] == 'o')
			{
				return false;
			}
			else if(gotOne && board[x][y] == p)
			{
				return true;
			}
			
			if(!gotOne)
				return false;
			
			y++;
			x--;
		}
		return false;
	}
	
	public void flipUp(char p, int x, int y)
	{
		y -= 1;
		while(y >= 0)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}
			
			y--;
		}
	}
	public void flipRight(char p, int x, int y)
	{
		x += 1;
		while(x < size)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}
			
			x++;
		}
	}
	public void flipLeft(char p, int x, int y)
	{
		x -= 1;
		while(x >= 0)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}
			
			x--;
		}
	}
	public void flipDown(char p, int x, int y)
	{		
		y += 1;
		while(y < size)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}			
			y++;
		}
	}
	public void flipUpRight(char p, int x, int y)
	{	
		y -= 1;
		x += 1;
		while(y >= 0 && x < size)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}			
			y--;
			x++;
		}
	}
	public void flipUpLeft(char p, int x, int y)
	{		
		y -= 1;
		x -= 1;
		while(y >= 0 && x >= 0)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}
			
			y--;
			x--;
		}
	}
	public void flipDownRight(char p, int x, int y)
	{		
		y += 1;
		x += 1;
		while(y < size && x < size)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}
			
			y++;
			x++;
		}
	}
	public void flipDownLeft(char p, int x, int y)
	{		
		y += 1;
		x -= 1;
		while(y < size && x >= 0)
		{
			if(board[x][y] != p && board[x][y] != 'o')
			{
				board[x][y] = p;
			}
			else if(board[x][y] == p)
			{
				break;
			}
			
			y++;
			x--;
		}
	}
	
	public char opposite(char p)
	{
		if(p == 'g')
			return 'b';
		else
			return 'g';
	}
	
	public int[][] state()
	{
		int[][] state = new int[size][size];
		for(int x = 0; x < size; x++)
        {
        	for(int y = 0; y < size; y++)
        	{
        		if(board[x][y] == 'g')
        			state[x][y] = -1;
        		else if(board[x][y] == 'b')
        			state[x][y] = 1;
        		else
        			state[x][y] = 0;
        	}
        }
        return state;
	}
}